/**
 * 
 * 
 * 命令模式处理器
 * 
 */
import { Action } from "./Action";
import Command from "./Command";

export default class CommandHandler {

    private undoStack: Command[];
    private redoStack: Command[];
    private action: Action;

    constructor() {
        this.undoStack = [];
        this.redoStack = [];
    }
    // 处理
    public handle(action: Action,command?: Command) {
        // 执行
        if(action === Action.EXEC) {
            command.exec();
            this.undoStack.push(command);
            this.redoStack = [];
        }
        // 撤销
        if(action === Action.UNDO && this.undoStack.length > 0) {
            let command: Command = this.undoStack.pop();
            command.undo();
            this.redoStack.push(command);
        } else {
            console.log("栈里面没有命令了");
        }
        // 重做
        if(action === Action.REDO && this.redoStack.length > 0) {
            let command: Command = this.redoStack.pop();
            command.redo();
            this.undoStack.push(command);
        }
    }

}